package com.zw.mes.module.prodtaskmanager.service;

import com.zw.mes.common.JsonResult;
import com.zw.mes.module.proStrucEdit.model.VwDsnProStrucModel;
import com.zw.mes.module.prodtaskmanager.entity.ProdOrd;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.prodtaskmanager.mapper.ProdOrdMapper;
import com.zw.mes.module.prodtaskmanager.model.ProdOrdModel;
import com.zw.mes.module.system.service.LogService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *项目串料标识维护腋窝逻辑层
 * @author XYH
 */
@Log4j2
@Service
public class ProdChangeFlagService {
    @Autowired
    private ProdOrdMapper mapper;
    private LogService logService;

    @Autowired
    private void setLogService(LogService logService) {
        this.logService = logService;
    }
    /**
     * 加载项目串料标识维护页面列表
     * @param param
     * @return
     */
    public List<ProdOrdModel> prodList(ProdOrdModel param) {
        return mapper.findChangeFlagProdOrdList(param);
    }

    /**
     * 保存编辑
     * @param param
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult save(ProdOrdModel param) {
        JsonResult jsonResult = new JsonResult();
        try {
            ProdOrd prodOrd = mapper.getProdOrdByPrimaryKey(param.getProdId());
            String beforeChangeStr = "";
            if(prodOrd.getProdChaMat()==null||prodOrd.getProdChaMat()==0){
                beforeChangeStr="是";
            }else{
                beforeChangeStr="否";
            }
            String beforeChangeStr2 = "";
            if(prodOrd.getProdChaPur()==null||prodOrd.getProdChaPur()==0){
                beforeChangeStr2="是";
            }else{
                beforeChangeStr2="否";
            }
            VwProject project = mapper.getVwProjectByProCode(prodOrd.getProdProCod());
            param.setProdProCod(prodOrd.getProdProCod());
            int result = mapper.updateProdOrdProdChaMatAndProdChaPurByProdProCod(param);
            if (result > 0) {
                String changeStr = "";
                if(param.getProdChaMat()==0){
                    changeStr="是";
                }else {
                    changeStr="否";
                }
                String changeStr2 = "";
                if(param.getProdChaPur()==0){
                    changeStr2="是";
                }else {
                    changeStr2="否";
                }
                logService.saveLog("项目串料标识维护", "项目编号为：【" + prodOrd.getProdProCod() + "】，项目名称为：【" + project.getProName() + "】的生产令编号为【" + prodOrd.getProdNo() + "的原材料串料标识从【"+beforeChangeStr+"】修改为【"+changeStr+"】，外购件串料标识从【"+beforeChangeStr2+"】修改为【"+changeStr2+"】。");
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("项目串料标识维护保存编辑异常：" + e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("保存失败，请联系管理员！");
        }
        return jsonResult;
    }
}
